unit imghelper;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, BGRABitmap, BGRABitmapTypes;

function ChangeHue(Original: TBGRACustomBitmap; Hue: Integer): TBGRABitmap;

implementation

function ChangeHue(Original: TBGRACustomBitmap; Hue: Integer): TBGRABitmap;
var
  Changed: TBGRABitmap;
  LargeHueShift: Integer;
  X, Y: Integer;
  PixelPtr: PBGRAPixel;
  HSLAPixel: THSLAPixel;
begin
  { HSLAPixel uses Hue from 0 to 65535 ($FFFF), while RPG Maker MV uses Hue
    from 0 to 360 }
  LargeHueShift := Hue * $FFFF div 360;
  Changed := TBGRABitmap.Create(Original);
  for Y := 0 to Changed.Height -1 do begin
    PixelPtr := Changed.ScanLine[Y];
    for X := 0 to Changed.Width -1 do begin
      HSLAPixel := BGRAToHSLA(PixelPtr^);

      { mod $10000 is implied because HSLAPixel.Hue is a Word value }
      HSLAPixel.Hue := HSLAPixel.Hue + LargeHueShift;
      PixelPtr^ := HSLAToBGRA(HSLAPixel);

      Inc(PixelPtr);
    end;
  end;
  Changed.InvalidateBitmap;

  ChangeHue := Changed;
end;

end.

